package com.wengzw.interceptor;

import com.wengzw.po.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURI();
        System.out.println(url);
        if(url.indexOf("/login")>=0 || url.indexOf("/statics")>=0 ||url.indexOf("/swagger-ui.html/**")>=0
                ||url.indexOf("/swagger-resources/**")>=0 ||url.indexOf("/webjars/**")>=0 ||url.indexOf("/v2/**")>=0
                || url.indexOf("/test")>=0)
        {
            return true;
        }
        HttpSession session = request.getSession();
        //System.out.print(session);
        User user = (User)session.getAttribute("loginUser");
        //System.out.print(user);
        if(user!=null){
            return true;
        }else{
            response.setContentType("text/html;charset=utf-8");
            response.sendRedirect(request.getContextPath()+"/login.html");
            return false;
        }

    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
